package com.allen.study.application.service;

import com.allen.study.application.api.assembler.ApprovalRecordDTOAssembler;
import com.allen.study.application.api.request.ApprovalRecordCreateRequest;
import com.allen.study.application.api.request.ApprovalRecordQueryRequest;
import com.allen.study.application.api.request.ApprovalRecordUpdateRequest;
import com.allen.study.application.api.response.ApprovalRecordQueryResponse;
import com.allen.study.application.api.response.ApprovalRecordResponse;
import com.allen.study.application.repository.IApprovalRecordReadModelRepo;
import com.allen.study.domain.entity.ApprovalRecord;
import com.allen.study.domain.service.ApprovalRecordService;
import com.allen.study.common.base.ApiPageResponse;
import com.allen.study.common.base.ApiResponse;
import com.allen.study.common.base.DomainResponse;
import com.allen.study.common.base.Pagination;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 审批记录表应用服务
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class ApprovalRecordAppService {

    /**
     * 审批记录表领域服务
     */
    private final ApprovalRecordService approvalRecordService;

    /**
     * 审批记录表读模型资源库
     */
    private final IApprovalRecordReadModelRepo approvalRecordReadModelRepo;

    /**
     * 审批记录表数据传输对象转换器
     */
    private final ApprovalRecordDTOAssembler approvalRecordDTOAssembler;

    /**
     * 创建审批记录表
     *
     * @param createRequest 审批记录表创建对象
     * @return 响应结果
     */
    public ApiResponse<Void> create(ApprovalRecordCreateRequest createRequest) {
        // 审批记录表创建对象 转成 审批记录表实体
        ApprovalRecord approvalRecord = approvalRecordDTOAssembler.assembler(createRequest);
        // 调用创建审批记录表领域服务
        DomainResponse<Void> domainResponse = approvalRecordService.create(approvalRecord);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键删除审批记录表
     *
     * @param approvalRecordId 审批记录表主键
     * @return 响应结果
     */
    public ApiResponse<Void> deleteById(Long approvalRecordId) {
        // 调用删除审批记录表领域服务
        DomainResponse<Void> domainResponse = approvalRecordService.deleteById(approvalRecordId);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键更新审批记录表
     *
     * @param approvalRecordId 审批记录表主键
     * @param updateRequest 审批记录表更新对象
     * @return 响应结果
     */
    public ApiResponse<Void> updateById(Long approvalRecordId, ApprovalRecordUpdateRequest updateRequest) {
        // 审批记录表更新对象 转成 审批记录表实体
        ApprovalRecord approvalRecord = approvalRecordDTOAssembler.assembler(updateRequest);
        // 设置主键
        approvalRecord.setId(approvalRecordId);
        // 调用更新审批记录表领域服务
        DomainResponse<Void> domainResponse = approvalRecordService.updateById(approvalRecord);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键查询审批记录表详情
     *
     * @param approvalRecordId 审批记录表主键
     * @return 响应结果
     */
    public ApiResponse<ApprovalRecordResponse> queryById(Long approvalRecordId) {
        // 根据主键查询审批记录表
        DomainResponse<ApprovalRecord> domainResponse = approvalRecordService.queryById(approvalRecordId);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse, approvalRecordDTOAssembler::toResponse);
    }

    /**
     * 分页查询审批记录表
     *
     * @param pagination   分页
     * @param queryRequest 审批记录表查询对象
     * @return 响应结果
     */
    public ApiPageResponse<ApprovalRecordQueryResponse> query(Pagination pagination, ApprovalRecordQueryRequest queryRequest) {
        // 调用审批记录表读模型资源库分页查询
        List<ApprovalRecordQueryResponse> list = approvalRecordReadModelRepo.query(pagination, queryRequest);
        return ApiPageResponse.ok(pagination, list);
    }
}
